import MsuiDataGridItemModel from "./msui-datagrid-item-model";
import { reactive } from "vue";

class MsuiDataGridModel {
  constructor(dataModel) {
    this.bind = {};
    this.vOn = {};
    this.initDataGridItem(dataModel);
  }
  initDataGridItem(dataModel, align) {
    this.bind.columns = [];
    this.bind.scroll = reactive({
      y: 200,
      scrollToFirstRowOnChange: true,
    });
    this.bind.dataSource = reactive([]);
    for (let key in dataModel) {
      let columns = new MsuiDataGridItemModel({
        key,
        align: "center",
        dataIndex: key,
        title: dataModel[key],
      });
      this.bind.columns.push(columns);
      this[key] = columns;
    }
  }
  setBind(obj) {
    if(obj.stripe) {
      obj.rowClassName = (record, index) => (index % 2 === 1 ? 'table-striped' : null)
    }
    this.bind = { ...this.bind, ...obj };
    return this;
  }
  setEvent(obj) {
    this.vOn = { ...this.vOn, ...obj };
    return this;
  }
  setData(array) {
    let data = this.bind.dataSource.splice(0, this.bind.dataSource.length);
    this.bind.dataSource.push(...array);
    return this;
  }
}

export default MsuiDataGridModel;
